/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package zaizai.com.slice;

import com.ajguan.ResourceTable;
import com.aries.ui.view.tab.listener.CustomTabEntity;
import com.aries.ui.view.tab.listener.OnTabSelectListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import zaizai.com.CommonTabLayout;

import java.util.ArrayList;

public class ViewPagerAbilitySlice extends AbilitySlice {

    /**
     * pager list
     */
    private ArrayList<EasyFraction> pagerList = new ArrayList<>();

    /**
     * 导航栏
     */
    private CommonTabLayout tab;
    /**
     * pageSlider
     */
    private PageSlider pageSlider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LayoutScatter
                .getInstance(ViewPagerAbilitySlice.this)
                .parse(ResourceTable.Layout_ability_page_slider, null, false));
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        Button titleBar = (Button) findComponentById(ResourceTable.Id_title_bar);
        titleBar.setFont(Font.DEFAULT_BOLD);
        tab = (CommonTabLayout) findComponentById(ResourceTable.Id_tab);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        ArrayList<CustomTabEntity> titles = getTitles();
        PageProvider pageProvider = new PageProvider();
        pageSlider.setProvider(pageProvider);
        tab.setTabData(titles);
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int position, float value, int next) {
            }

            @Override
            public void onPageSlideStateChanged(int position) {
            }

            @Override
            public void onPageChosen(int position) {
                tab.setCurrentTab(position);
            }
        });

        // 重写拖拽监听禁止上下滑动事件
        pageSlider.setDraggedListener(Component.DRAG_VERTICAL, new Component.DraggedListener() {
            @Override
            public void onDragDown(Component component, DragInfo dragInfo) {
            }

            @Override
            public void onDragStart(Component component, DragInfo dragInfo) {
            }

            @Override
            public void onDragUpdate(Component component, DragInfo dragInfo) {
            }

            @Override
            public void onDragEnd(Component component, DragInfo dragInfo) {
            }

            @Override
            public void onDragCancel(Component component, DragInfo dragInfo) {
            }
        });
        tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                pageSlider.setCurrentPage(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        pageSlider.setCurrentPage(0);

    }

    /**
     * 设置数据
     *
     * @return ArrayList title列表数据
     */
    private ArrayList<CustomTabEntity> getTitles() {
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT
        );
        ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
        for (int index = 0; index < 4; index++) {
            final int postion = index;
            customTabEntities.add(new CustomTabEntity() {
                @Override
                public String getTabTitle() {
                    return "PAGER:" + postion + 1;
                }

                @Override
                public int getTabSelectedIcon() {
                    return 0;
                }

                @Override
                public int getTabUnselectedIcon() {
                    return 0;
                }
            });
            EasyFraction easyFraction = new EasyFraction(ViewPagerAbilitySlice.this);
            easyFraction.setLayoutConfig(layoutConfig);
            pagerList.add(easyFraction);
        }
        return customTabEntities;
    }

    /**
     * pagerslider provideer
     */
    public class PageProvider extends PageSliderProvider {


        @Override
        public int getCount() {
            return pagerList == null ? 0 : pagerList.size();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int position) {
            EasyFraction easyFraction = pagerList.get(position);
            componentContainer.addComponent(easyFraction);
            easyFraction.notifyDataChanged();
            return componentContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int index, Object object) {
            componentContainer.removeComponent(pagerList.get(index));
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return false;
        }
    }

}
